﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using Storage.RemoteStorage.Sevice.Entity;
using System.IO;
using Storage.RemoteStorage.Client;
using System.Diagnostics;
using StorageManager.Interface;

namespace TestTransfert
{
    public partial class Form1 : Form
    {
        WCFConnector Connector = null;

        public Form1()
        {
            InitializeComponent();

            Connector = new WCFConnector();
            Connector.ReportProgress += new ReportProgress(Connector_ReportProgress);
        }

        void Connector_ReportProgress(object Sender, ProgressStreamReportEventArgs e)
        {
            progressBar1.Maximum = Convert.ToInt32(e.StreamLength);
            progressBar1.Value = Convert.ToInt32(e.StreamPosition);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            VirtualFolder folder = Connector.GetRootFolder();
            TreeNode root = new TreeNode(folder.Name);
            root.Tag = folder;
            FillTreeView(ref root);
            treeView1.BeginUpdate();
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(root);
            treeView1.EndUpdate();
        }

        private void FillTreeView(ref TreeNode node)
        {
            VirtualFolder folder = node.Tag as VirtualFolder;
            if (!folder.Name.Equals("Root"))
                folder = Connector.GetRemoteFolder(folder.Name, folder.Parent);
            if (folder.nbFiles > 0)
            {
                bool End = false;
                int Index = 1;
                int PageSize = 30;
                do
                {

                    VirtualFile[] Files = Connector.GetRemoteFilePaged(folder, Index, PageSize, out End);
                    foreach (VirtualFile item in Files)
                    {
                        TreeNode Fnode = node.Nodes.Add(item.Name);
                        Fnode.Tag = item;
                    }
                    Index++;

                } while (!End);
            }
            if (folder.nbSubFolder > 0)
            {
                bool End = false;
                int Index = 1;
                int PageSize = 30;
                System.Diagnostics.Stopwatch timer = new Stopwatch();
                timer.Start();
                Debug.WriteLine(string.Format("Start loading {0} from {1} at {2}",folder.nbSubFolder,folder.Name,timer.Elapsed));
                do
                {

                    Debug.WriteLine(string.Format("Start loading Page {3}/{0} from {1} at {2}", folder.nbSubFolder, folder.Name, timer.Elapsed,Index));
                    VirtualFolder[] subFolders = Connector.GetRemoteFolderPaged(folder, Index, PageSize, out End);
                    //if (subFolders.Length == PageSize)
                    //    End = true;
                    foreach (VirtualFolder item in subFolders)
                    {
                        TreeNode Tnode = node.Nodes.Add(item.Name, item.Name);
                        Tnode.Tag = item;
                        FillTreeView(ref Tnode);
                    }
                    Debug.WriteLine(string.Format("End loading Page {3}/{0} from {1} at {2}", folder.nbSubFolder, folder.Name, timer.Elapsed, Index));
                    Index++;
                    if (Index > 2)
                        break;

                } while (!End);
                 Debug.WriteLine(string.Format("End loading {0} from {1} at {2}",folder.nbSubFolder,folder.Name,timer.Elapsed));
            }
        }

        //private void FillTreeViewLevel1(ref TreeNode node)
        //{
        //    VirtualFolder folder = node.Tag as VirtualFolder;
        //    if (!folder.Name.Equals("Root"))
        //        folder = Connector.GetRemoteFolder(folder.Name, folder.Parent);
        //    if (folder.Files != null)
        //        foreach (VirtualFile item in folder.Files)
        //        {
        //            TreeNode Fnode = node.Nodes.Add(item.Name);
        //            Fnode.Tag = item;
        //        }
        //    if (folder.SubFolder != null)
        //        foreach (VirtualFolder item in folder.SubFolder)
        //        {
        //            TreeNode Tnode = node.Nodes.Add(item.Name, item.Name);
        //            Tnode.Tag = item;

        //        }
        //}

        private void cmd_createfolder_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                VirtualFolder folder = treeView1.SelectedNode.Tag as VirtualFolder;
                if (folder != null)
                {
                    VirtualFolder Cihld = Connector.CreateFolder(txtfolderName.Text, folder);
                    treeView1.SelectedNode.Nodes.Add(Cihld.Name, Cihld.Name);

                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                VirtualFolder folder = treeView1.SelectedNode.Tag as VirtualFolder;
                if (folder != null)
                {
                    if (Connector.DeleteFolder(treeView1.SelectedNode.Text, folder.Parent))
                    {
                        treeView1.SelectedNode.Remove();
                    }
                }
            }
        }

        private void cmd_download_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Tag is VirtualFile)
                {
                    VirtualFile file = treeView1.SelectedNode.Tag as VirtualFile;

                    if (file != null)
                    {
                        FolderBrowserDialog save = new FolderBrowserDialog();
                        if (save.ShowDialog() == DialogResult.OK)
                        {
                            Connector.downloadFile(file, save.SelectedPath);
                        }


                    }
                }
                else if (treeView1.SelectedNode.Tag is VirtualFolder)
                {
                    VirtualFolder folder = treeView1.SelectedNode.Tag as VirtualFolder;

                    if (folder != null)
                    {
                        FolderBrowserDialog save = new FolderBrowserDialog();
                        if (save.ShowDialog() == DialogResult.OK)
                        {
                            Stopwatch wd = new Stopwatch();
                            wd.Start();
                            Connector.DownloadFolder(folder, new DirectoryInfo(save.SelectedPath), true, true);
                            wd.Stop();
                            MessageBox.Show(wd.Elapsed.ToString("G"));
                        }


                    }

                }
            }
        }

        private void cmd_Upload_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                VirtualFolder folder = treeView1.SelectedNode.Tag as VirtualFolder;
                if (folder != null)
                {

                    OpenFileDialog save = new OpenFileDialog();
                    if (save.ShowDialog() == DialogResult.OK)
                    {
                        FileInfo newFile = new FileInfo(save.FileName);
                        //if (transfermanager == null)
                        //    transfermanager = new TransferServiceClient("TransfertTCPEndPoint");
                        //using (ProgressStream str = new ProgressStream(newFile.OpenRead()))
                        //{
                        //    progressBar1.Value = 0;
                        //    progressBar1.Maximum = Convert.ToInt32(newFile.Length);
                        //    str.BytesRead += new ProgressStreamReportDelegate(str_BytesRead);
                        //    transfermanager.UploadFile(
                        //    Path.Combine(folder.RelativePath, newFile.Name),
                        //    newFile.Length, str);
                        //}
                        Connector.UploadFile(folder, newFile);

                    }
                }
            }
        }

        void str_BytesRead(object sender, ProgressStreamReportEventArgs args)
        {
            progressBar1.Value = Convert.ToInt32(args.StreamPosition);
            Application.DoEvents();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            VirtualFolder folder = Connector.GetRemoteFolderbyPath(txtPath.Text, null, false);
            TreeNode root = new TreeNode(folder.Name);
            root.Tag = folder;
            FillTreeView(ref root);
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(root);
        }

        private void cmd_uploadFolder_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                VirtualFolder folder = treeView1.SelectedNode.Tag as VirtualFolder;
                if (folder != null)
                {

                    FolderBrowserDialog save = new FolderBrowserDialog();
                    if (save.ShowDialog() == DialogResult.OK)
                    {
                        DirectoryInfo NewFolder = new DirectoryInfo(save.SelectedPath);
                        Stopwatch wd = new Stopwatch();
                        wd.Start();
                        Connector.UploadFolder(folder, NewFolder, true);
                        wd.Stop();
                        MessageBox.Show(wd.Elapsed.ToString("G"));

                    }
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            VirtualFolder folder = Connector.GetRemoteFolderbyPath(txtPath.Text, null, false);
            TreeNode root = new TreeNode(folder.Name);
            root.Tag = folder;
            int Index = Convert.ToInt32(txtIndex.Text);
            VirtualFolder subFolder = Connector.GetRemoteFolderbyIndex(Index, folder);
            if (subFolder != null)
            {
                TreeNode SubFolder = new TreeNode(subFolder.Name);
                SubFolder.Tag = folder;
                root.Nodes.Add(SubFolder);
                treeView1.Nodes.Clear();
                treeView1.Nodes.Add(root);
            }
        }

        private void cmd_Check_Click(object sender, EventArgs e)
        {
            try
            {
                Connector.InitServiceDownload();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
